package com.aglhz.nature.presenter;

import java.util.List;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import android.util.Log;

import com.aglhz.nature.constants.ServerAPI;
import com.aglhz.nature.events.SecondClassificationViewEvent;
import com.aglhz.nature.events.SecondHomeReEvent;
import com.aglhz.nature.modle.SecondClassificationImage;
import com.aglhz.nature.modle.ShopListBean;
import com.aglhz.nature.modle.item.ShopListData;
import com.aglhz.nature.modle.item.classification.SecondClassificationListData;
import com.aglhz.nature.presenter.base.BasePresenter;
import com.aglhz.nature.view.iv.SecondRETitleView;
import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;

public class SecondHomeREPresenter extends BasePresenter{
	private static String TAG = SecondHomeREPresenter.class.getName();
	
	private SecondRETitleView secondRETitleView;
	private ShopListBean shopListBean;
	private List<ShopListData> shopListData;
	public SecondHomeREPresenter(SecondRETitleView view) {
		super();
		secondRETitleView = view;
	}
	
	
	// /*********************data operation*******************///
		public void loadData() {
			// load remote data
			String urlId = secondRETitleView.setTagId();

			this.loadShopListBean(urlId);

			// save remote data

		}
		
	
		// /********************view operation*******************///
		@Subscribe(threadMode = ThreadMode.MAIN)
		public void onEventSecondHomeREThread(
				SecondHomeReEvent event) {

			String msg = event.getMessage();
			Log.i(TAG, "SecondHomeReEvent:" + msg);
			// update UI By event
			if (SecondHomeReEvent.EVENT_UPDATE_TITLE_LISTVIEW == msg) {
				// update grid view
				Log.d(TAG, "secondclassificationImage---yyyyyyyyyyyyy---"
						+ shopListBean.getData());

				this.showListView();

				Log.d(TAG, "secondclassificationImage---gggggggggggggggg---"
						+ shopListBean.getData());
			}

		}
		
		public void showListView(){
			if(0==shopListBean.getData().size()){
				secondRETitleView.showErrorView();
			}else{
			secondRETitleView.setAdapeter(shopListBean.getData());
		}
		
		}
		
		
		
		public void loadShopListBean(String urlId){
			String url=ServerAPI.APIHEARD+ServerAPI.HOME_SECOND_RECOMMEND+"?recommendGoodsTagId="+urlId;
			AsyncHttpClient client = new AsyncHttpClient();
			client.get(url, new TextHttpResponseHandler() {

				@Override
				public void onSuccess(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString) {
					try {
						Log.e(TAG, "response" + responseString);

						Gson gson = new Gson();

						ShopListBean sshopListBean = gson
								.fromJson(responseString,
										ShopListBean.class);
						shopListBean = (ShopListBean) sshopListBean
								.clone();

						shopListData= shopListBean.getData();

						Log.d(TAG, "shopListData---mcp-----"
								+ shopListData);
						// send event
						SecondHomeReEvent event = new SecondHomeReEvent(
								SecondHomeReEvent.EVENT_UPDATE_TITLE_LISTVIEW);
						EventBus.getDefault().post(event);

						Log.e(TAG, "success----------------------------"
								+ "success");
					} catch (Exception e) {
						e.printStackTrace();
					}

				}

				@Override
				public void onFailure(int statusCode,
						cz.msebera.android.httpclient.Header[] headers,
						String responseString, Throwable throwable) {

					Log.e(TAG, "failure");
				}

			});

		}
	
}
